<?
	/**
	 * UCLibHttp.php
	 * SDO UC项目公共类库 - Http Socket请求
	 *
	 * Copyright (c) 2010 snda.com - Web Development Center
	 * @authos: Gu Shougang <gushougang@snda.com>
	 */
	 
	class UCLibHttp
	{
		/*
		 * 请求资源的地址
		 */
		private $_url = '';
		/*
		 * post参数字符串
		 */
		private $_params = '';
		/*
		 * header参数字符串
		 */
		private $_headers = '';
		/*
		 * 主机
		 */
		private $_host = '';
		/*
		 * 端口号
		 */
		private $_port = ''; 
		/*
		 * 资源路径
		 */
		private $_path = '';
		/*
		 * 查询字符串
		 */
		private $_query = '';
		/*
		 * 请求超时时间, 单位s
		 */ 
		private $_time_out = 10;
		/*
		 * 相应信息
		 */
		private $_response = array();
		/*
		 * 版本
		 */
		private $_version = '0.1'; 
		/*
		 * 错误代码
		 */
		public $err_no = '';
		/*
		 * 错误描述 
		 */
		public $err_str = '';

		/** 
		 * 构造函数 
		 * @param string $url 请求地址
		 */ 
		public function UCLibHttp($url = '')
		{ 
			$this->__construct($url);
		}
		
		/**
		 * 构造方法
		 * @param string $url 请求地址
		 */
		public function __construct($url = '')
		{
			$this->_url = $url;
		}
		
		/**
		 * 析构方法
		 */
		public function __destruct()
		{
			// ... 
		}
	 
		/**
		 * 设置url
		 * @param string $url 请求地址
		 */
		public function setURL($url = '')
		{
			$this->_url = $url;
		}

		/**
		 * 设置请求超时时间
		 * @param int $timeout 超时时间,单位s
		 */
		public function setTimeOut($timeout = 10)
		{
			$this->_time_out = $timeout;
		}

		/**
		 * 重置
		 */
		public function clear()
		{
			$this->_url = '';
			$this->_params = '';
			$this->_headers = '';
			$this->err_no = '';
			$this->err_str = '';
		}

		/**
		 * 添加header
		 */ 
		public function addHeader($str_header)
		{
			$this->_headers .= $str_header . '\n';
		}
		
		/**
		 * 设置header
		 */
		public function setHeader($str_header)
		{
			$this->_headers = $str_header . '\n';
		}
		
		/** 
		 * 添加get参数 
		 */ 
		public function addGetParam($arrParam)
		{
			$this->_url .= strpos($this->_url, '?') === false ? '?' : '&';
			$this->_url .= http_build_query($arrParam);
		}
		
		/**
		 * 添加post参数
		 */
		public function addPostParam($arrParam)
		{
			$this->_params .= strlen($this->_params) > 0 ? '&' : '';
			$this->_params .= http_build_query($arrParam);
		}

		/**
		* 重新添加post参数
		*/
		public function setPostParam($arrParam)
		{
			$this->_params = http_build_query($arrParam);
		}
		
		/**
		 * get方法
		 * 资源不存在时将返回空
		 */
		public function get()
		{
			return $this->_request('GET');
		}
		
		/** 
		* post方法
		* 资源不存在时将返回空
		*/
		public function post()
		{
			return $this->_request('POST');
		}
		
		/**
		 * getResponse方法
		 * @param $item [header | content]
		 */
		public function getResponse($item = '')
		{
			return $item == '' ? $this->_response : $this->_response[$item];
		}
		
		/**
		 * getContent方法
		 */
		public function getContent()
		{
			return $this->_response['content'];
		}
		
		/**
		 * getVersion方法
		 */
		public function getVersion()
		{
			return $this->_version;
		}
		
		/**
		 * 只检查post方法是否出错
		 */
		public function is_error()
		{
			return $this->err_no;
		}
		
		/**
		 * 返回出错信息
		 */
		public function get_error()
		{
			return $this->err_str;
		}
		
		/**
		 * 分解URL
		 */
		private function _parse_url()
		{
			$arr_url = parse_url($this->_url);
			if(!is_array($arr_url))
			{
				$arr_url = array();
			}
			$this->_host = $arr_url['host'];
			$this->_port = isset($arr_url['port']) ? $arr_url['port'] : '80';			
			$this->_path = !empty($arr_url['path']) ? $arr_url['path'] : '/';
			$this->_query = $arr_url['query'];
		}
		
		/**
		 * _request方法
		 */
		private function _request($method = 'GET')
		{
			$this->_parse_url();
			$fp = @fsockopen($this->_host, $this->_port, $this->err_no, $this->err_str, $this->_time_out);

			if(!$fp)
			{
				return false;
			}
			else
			{
				$request = '';
				$request .= sprintf("%s %s%s%s HTTP/1.0\n", $method, $this->_path, $this->_query ? "?" : "", $this->_query);
				$request .= "Host: " . $this->_host . "\n";
				$request .= $this->_headers;
				$request .= $method == "POST" ? "Content-type: application/x-www-form-urlencoded\n" : "";
				$request .= $method == "POST" ? "Content-length: " . strlen($this->_params) . "\n" : "";
				$request .= "Connection: close\n";
				$request .= $method == "POST" ? "\n$this->_params\n" : "";
				$request .= "\n";

				fputs($fp, $request);

				$results = "";

				while(!feof($fp))
				{
					$line = fgets($fp, 1024);
					$results .= $line;
				}
				fclose($fp);
				$this->_response['header'] = substr($results, 0, strpos($results,"\r\n\r\n") + 1);	//相应的头信息
				$this->_response['content'] = substr($results, strpos($results,"\r\n\r\n") + 4);		//去掉请求返回的头部
				
				return true;
			}
		}
	}
?>